package com.stony.core.struts.tag;

import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.taglib.TagUtils;

import com.eaoa.vo.pub.PubAccount;

/**
 * @author bobby.dou
 * 
 */
public class PermissionTag extends TagSupport {

	protected String permissionid = null;

	protected String scope = null;

	protected String inverse = "N";

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
	 */
	public int doEndTag() throws JspException {

		return (EVAL_PAGE);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
	 */
	public int doStartTag() throws JspException {
		Long permissionno = new Long(getPermissionid());

		PubAccount account =   (PubAccount)TagUtils.getInstance().lookup(pageContext,
				"Employee", "session");
		
		Set permissionids = account.getPermissionids();

		if (permissionids != null){
			if(inverse.equals("N") && permissionids.contains(permissionno)){
				return (EVAL_BODY_INCLUDE);
			}else if(inverse.equals("Y") && !permissionids.contains(permissionno)){
				return (EVAL_BODY_INCLUDE);
			}
		}

		return (SKIP_BODY);

	}

	/**
	 * Release all allocated resources.
	 */
	public void release() {

		super.release();
		permissionid = null;
		scope = null;

	}

	public String getPermissionid() {
		return permissionid;
	}

	public void setPermissionid(String permissionid) {
		this.permissionid = permissionid;
	}

	/**
	 * @return the scope
	 */
	public String getScope() {
		return scope;
	}

	/**
	 * @param scope
	 *            the scope to set
	 */
	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getInverse() {
		return inverse;
	}

	public void setInverse(String inverse) {
		this.inverse = inverse;
	}

}
